package com.jdd.modules.deviceConf.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jdd.common.api.vo.Result;
import com.jdd.common.util.ResultGenerator;
import com.jdd.modules.deviceConf.entity.TDeviceConf;
import com.jdd.modules.deviceConf.mapper.TDeviceConfMapper;
import com.jdd.modules.deviceConf.service.TDeviceConfService;
import com.jdd.modules.deviceConf.vo.TDeviceConfTreeModal;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * @Author: LS
 * @Date: 2022 - 09 - 27 - 14:31
 * @Description:
 * @version: 1.0
 */
@Slf4j
@Controller
@RestController("TDeviceConfController")
@RequestMapping("/deviceConf")
public class TDeviceConfController {

    @Resource
    private TDeviceConfService tDeviceConfService;

    @Resource
    private TDeviceConfMapper tDeviceConfMapper;

    @RequestMapping(value = "/queryTreeList", method = RequestMethod.GET)
    public Result<List<TDeviceConfTreeModal>> queryConfTree(){
        Result<List<TDeviceConfTreeModal>> result = new Result<>();
        try {
            List<TDeviceConfTreeModal> list =  tDeviceConfService.queryTreeList();
            result.setResult(list);
            result.setSuccess(true);
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }
        return result;
    }
    @RequestMapping(value = "/findConf",method = RequestMethod.GET)
    public Result<Page<TDeviceConf>> findConf(@RequestParam(name="id")String id,@RequestParam(name = "pageNo",defaultValue = "1")Integer pageNo,@RequestParam(name = "pageSize",defaultValue = "10")Integer pageSize){
        Result<Page<TDeviceConf>> result = new Result<>();
        LambdaQueryWrapper<TDeviceConf> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TDeviceConf::getDeviceId,id);
        Page<TDeviceConf> page = new Page<TDeviceConf>(pageNo,pageSize);
        tDeviceConfMapper.selectPage(page,wrapper);
        result.setResult(page);
        result.setSuccess(true);
        return result;
    }
    @RequestMapping(value ="/copyConf",method = RequestMethod.GET)
    public Result copyConf(@RequestParam(name="copyDeviceId")String copyDeviceId,@RequestParam(name = "copyedDeviceId")String copyedDeviceId){
        try {
            LambdaQueryWrapper<TDeviceConf> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(TDeviceConf::getDeviceId,copyDeviceId);
            List<TDeviceConf> list = tDeviceConfService.list(wrapper);
            for (TDeviceConf tDeviceConf : list) {
                tDeviceConf.setId(null);
                tDeviceConf.setDeviceId(copyedDeviceId);
                tDeviceConfService.save(tDeviceConf);
            }
            return ResultGenerator.genSuccessResult("复制配置成功");
        } catch (Exception e) {
           log.info("-----复制配置时发生错误-----{}",e);
            return ResultGenerator.genFailResult("复制失败");
        }

    }

    @DeleteMapping(value = "delConf")
    public Result delConf(@RequestParam(value = "id",required = true)String id){
        try {
            tDeviceConfService.removeById(id);
            return ResultGenerator.genSuccessResult("删除成功");
        } catch (Exception e) {
            log.error("删除配置失败{}",e);
            return ResultGenerator.genFailResult("删除配置出现错误");
        }
    }

    @DeleteMapping("delBatch")
    public Result delBatchConf(@RequestParam(value = "ids",required = true)String ids){
        Result<TDeviceConf> result = new Result<TDeviceConf>();
        if (ids == null || "".equals(ids.trim())) {
            result.error500("参数不识别！");
        } else {
            this.tDeviceConfService.removeByIds(Arrays.asList(ids.split(",")));
            result.success("删除成功!");
        }
        return result;
    }

    @RequestMapping(value = "addConf",method = RequestMethod.POST)
    public Result addConf(@RequestBody TDeviceConf tDeviceConf){
        Result result = new Result();
        try {
            log.debug("拿到的时间:{}",tDeviceConf.getStartTime());
            /*1.是时间段配置*/
            if (tDeviceConf.getType()==1) {
                getTime(tDeviceConf);
            }else{
                getDate(tDeviceConf);
            }
            tDeviceConfService.saveOrUpdate(tDeviceConf);
            result.success("操作成功");
        }catch (Exception e){
            log.error("-----添加或修改设备配置失败-----{}",e);
            result.error500("操作失败");
        }
        return result;
    }

    private void getDate(TDeviceConf tDeviceConf) {
        String[] startDate = tDeviceConf.getStartTime().split(" ");
        String[] endDate = tDeviceConf.getEndTime().split(" ");
        getIntTime(tDeviceConf, startDate[1], endDate[1]);
        String[] dateStart = startDate[0].split("-");
        String[] dateEnd = startDate[0].split("-");
        tDeviceConf.setMonth(Integer.parseInt(dateStart[1]+dateEnd[1]));
        tDeviceConf.setDay(Integer.parseInt(dateStart[2]+dateEnd[2]));
    }



    private void getTime(TDeviceConf tDeviceConf) {
        getIntTime(tDeviceConf, tDeviceConf.getStartTime(), tDeviceConf.getEndTime());
    }

    private void getIntTime(TDeviceConf tDeviceConf, String s, String s2) {
        String[] startTime = s.split(":");
        String[] endTime = s2.split(":");
        tDeviceConf.setHour(Integer.parseInt(startTime[0] + endTime[0]));
        tDeviceConf.setMinute(Integer.parseInt(startTime[1] + endTime[1]));
        tDeviceConf.setSecond(Integer.parseInt(startTime[2] + endTime[2]));
    }
}

